﻿using System;
using System.Xml.Linq;
using System.Windows;
using V2TEK.NET.SH.Silverlight.Charts.Core.Schedule;
using System.Collections.ObjectModel;

namespace V2TEK.NET.SH.Silverlight.Charts
{
    /// <summary>
    /// 增加了IConstraintLayout接口，但是V1.0.0.0中不予实现
    /// </summary>
    public abstract class AbstractEntryLayout<T,R> : TimeLayout<T>, IEntryLayout<T, R>
    {
        public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource", typeof(object), typeof(AbstractEntryLayout<T,R>), new PropertyMetadata(null, new PropertyChangedCallback(OnDataSourceChange)));
        public static readonly DependencyProperty RowHeightProperty = DependencyProperty.Register("RowHeight", typeof(double), typeof(AbstractEntryLayout<T, R>), new PropertyMetadata(20d));
        public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(ObservableCollection<R>), typeof(AbstractEntryLayout<T, R>), new PropertyMetadata(new ObservableCollection<R>()));

        public AbstractEntryLayout()
        {
            this.Items = new ObservableCollection<T>();
        }

        public ObservableCollection<R> Rows 
        {
            get
            {
                return (ObservableCollection<R>)GetValue(RowsProperty);
            }
            set
            {
                SetValue(RowsProperty, value);
            }
        }

        public event EventHandler BatchRowsChanged;

        protected void RaiseBatchRowsChanged()
        {
            if(this.BatchRowsChanged != null)
                BatchRowsChanged(this,new EventArgs());
        }

        public int RowCount
        {
            get
            {
                return Rows.Count;
            }
        }


        #region IEntryLayout Members

        /// <summary>
        /// 设置数据源会触发Data集合更改事件，主动调用CreateLayout()方法，设置数据源后，请调用Update()重绘图形
        /// </summary>
        public virtual object DataSource
        {
            get { return GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        protected virtual void DataBind()
        {
            if (DataSource == null)
            { 
                throw new NullReferenceException("The DataSource can not be nullable before using DataBind()"); 
            }
        }

        public double RowHeight
        {
            get { return (double)GetValue(RowHeightProperty); }
            set { SetValue(RowHeightProperty, value); }
        }

        public virtual void CreateLayout()
        {
            //SetValue(AbstractEntryLayout<T,R>.RowsProperty, new ObservableCollection<R>()); 
            Rows.Clear();
        }

        #endregion

        protected static void OnDataSourceChange(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as AbstractEntryLayout<T,R>).DataBind();
        }

    }
}
